package br.com.rotary.utilitario;

import java.text.ParseException;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author laverson
 */
public class Caracter {

    public static String cpfMask = "###.###.###-##";
    public static String cnpjMask = "##.###.###/####-##";
    public static String cepMask = "##.###-###";
    public static String phoneMask = "(##) ####-####";
    public static String inscricaoCadastralMask = "##.##.###.####.##";

    public static String removecaracter(String s) {
        if (s != null) {
            s = s.replaceAll("\\.", "").replace("-", "").replace("/", "").replace("-", "").replace("[", "").replace("]", "").replace("{", "").replace("}", "");
        }
        return s;
    }

    /**
     * Remove caracteres não alphanuméricos, exceto o ponto (.)
     *
     * @param s
     * @return string com os caracteres removidos
     */
    public static String removeCaracterIgnoraPonto(String s) {
        if (s != null) {
            s = s.replace("-", "").replace("/", "").replace("-", "").replace("[", "").replace("]", "").replace("{", "").replace("}", "");
        }
        return s;
    }

    public static String removerAcentos(String s) {
        if (s != null) {
            s = s.replaceAll("[ãâàáä]", "a")
                    .replaceAll("[êèéë]", "e")
                    .replaceAll("[îìíï]", "i")
                    .replaceAll("[õôòóö]", "o")
                    .replaceAll("[ûúùü]", "u")
                    .replaceAll("[ÃÂÀÁÄ]", "A")
                    .replaceAll("[ÊÈÉË]", "E")
                    .replaceAll("[ÎÌÍÏ]", "I")
                    .replaceAll("[ÕÔÒÓÖ]", "O")
                    .replaceAll("[ÛÙÚÜ]", "U")
                    .replace('ç', 'c')
                    .replace('Ç', 'C')
                    .replace("[ñǹń]", "n")
                    .replace("[ÑŃǸ]", "N");
        }

        return s;
    }

    public static String substituirEspacos(String s) {
        if (s != null) {
            s = s.replace(' ', '_');
        }

        return s;
    }

    public static String addMask(String s, String mask) {
        MaskFormatter maskFormatter;

        try {
            maskFormatter = new MaskFormatter(mask);
            maskFormatter.setValueContainsLiteralCharacters(false);
            return maskFormatter.valueToString(s);
        } catch (ParseException ex) {
            System.err.println(ex);
            throw new RuntimeException(ex);
        }
    }
}
